package com.read.operationsapi.filter;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
public class DomainFilter implements Filter {

    public void init(FilterConfig filterConfig) throws ServletException {
     log.info("app 域名过滤器初始化");
    }


    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {


        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(servletRequest, servletResponse);


//
//        String temp = "";
//        String decryptStr = "";
//        try {
//
//            if(!ObjectUtils.isEmpty(servletRequest)) {
//                BufferedReader br = servletRequest.getReader();
//                System.out.println(br.readLine());
//                while ((temp = br.readLine()) != null) {
//                    decryptStr = decryptStr + temp;
//                }
//            }
//        } catch (NullPointerException e) {
//            System.out.println("空空空");
//            return;
//        }
//        System.out.println("传过来的是:"+decryptStr);
//        System.out.println("进入了 DomainFilter");
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
//        response.setHeader("Access-Control-Max-Age", "3600");
//        response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//        chain.doFilter(servletRequest, servletResponse);
    }

    public void destroy() {

    }


}
